//
//  WaitPublishLoginView.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/1.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "WaitPublishLoginView.h"
#import "MZTimerLabel.h"
#import "HistoryNumView.h"
#import "RolesVC.h"
#import "PdcDetailVC.h"
#import "WYPublicWeb.h"
//计算详细
#define JSXX_URL @"api/ApiGoods/getGameRulesDetail?id="
//商品详情
#define DETAIL_URL @"/api/ApiGoods/getGoodsDetail"
@interface WaitPublishLoginView ()
{
    __weak IBOutlet UILabel *jxzLab;
    __weak IBOutlet UILabel *titleLab;
    __weak IBOutlet UIView *bgView;
    __weak IBOutlet UILabel *timeLab;
    __weak IBOutlet UIButton *jsBtn;
    __weak IBOutlet UILabel *numLab;
    __weak IBOutlet UIView *bgView2;
    __weak IBOutlet UIButton *showBtn;
    
    __weak IBOutlet UILabel *jxLab;
    
    __weak IBOutlet UILabel *tsLab;
    
    
    //倒计时
    MZTimerLabel *timerExample1;
}
@end
@implementation WaitPublishLoginView

- (void)awakeFromNib
{
    if(WYMWITH > 320){
        tsLab.font = [UIFont systemFontOfSize:14];
    }
    jxzLab.textColor = QBB_RED_COLOR;
    jxzLab.layer.borderWidth = 0.5;
    jxzLab.layer.borderColor = QBB_RED_COLOR.CGColor;
    jxzLab.layer.masksToBounds = YES;
    jxzLab.layer.cornerRadius = 2;
    bgView.layer.cornerRadius = 2;
    bgView.backgroundColor = QBB_RED_COLOR;
    bgView.layer.masksToBounds = YES;
    jsBtn.layer.cornerRadius = 2;
    jsBtn.layer.masksToBounds = YES;
    jsBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    jsBtn.layer.borderWidth = 1;
    bgView.layer.cornerRadius = 2;
    bgView.layer.masksToBounds = YES;
    showBtn.layer.cornerRadius = 2;
    showBtn.layer.masksToBounds = YES;
    showBtn.backgroundColor = QBB_RED_COLOR;
    bgView2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Dbb_backColor"]];

}
//填充view数据的方法
- (void)fillViewWithDic:(NSDictionary *)dic
{
    titleLab.text = [NSString stringWithFormat:@"(第%@期)%@",dic[@"goods"][@"qishu"],dic[@"goods"][@"title"]];
    self.pdc_id = dic[@"goods"][@"id"];
    self.pdc_qishu = dic[@"goods"][@"qishu"];
    NSNumber *canjia = dic[@"goods"][@"your_go_total"];
    numLab.text = [NSString stringWithFormat:@"%ld",(unsigned long)[canjia integerValue]];
    NSNumber *t_n = dic[@"goods"][@"countdown"];
    if ([t_n integerValue] >= 0  ) {
        //倒计时处理
        timerExample1 = [[MZTimerLabel alloc] initWithLabel:timeLab andTimerType:MZTimerLabelTypeTimer];
        [timerExample1 setCountDownTime:[t_n integerValue]+1];
        timerExample1.resetTimerAfterFinish = YES;
        timerExample1.timeFormat = @"mm:ss:SS";
        
        if(![timerExample1 counting]){
            
            [timerExample1 startWithEndingBlock:^(NSTimeInterval countTime) {
                timeLab.hidden = YES;
                jxLab.text = @"正在开奖";
                
                [self requestForDetail];
            }];
        }
        
    }else{
        timeLab.hidden = YES;
        jxLab.text = @"正在开奖";
        
    }

}
//获取控制器
- (UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}
//获取商品详情接口
- (void)requestForDetail
{
    if (self.pdc_id != nil && self.pdc_id.length != 0) {
        NSDictionary *params = @{@"id":self.pdc_id};
        [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,DETAIL_URL] Parameters:params successBlock:^(NSDictionary *dic) {
            NSDictionary *pdc = dic[@"data"][@"goods"];
            
            NSString *pdc_open_time_flag = pdc[@"q_showtime"];
            if ([pdc_open_time_flag isEqualToString:@"Y"]) {
                double delayInSeconds = 1.0;
                __block WaitPublishLoginView* bself = self;
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    
                    [bself requestForDetail];
                });

                return ;
            }
            PdcDetailVC *detailVC = (PdcDetailVC*)[self viewController];
            detailVC.pdc_id = self.pdc_id;
            
            if ([[WYPublic GetCurrentviewController]class] == [PdcDetailVC class]) {
                [self removeFromSuperview];
                [detailVC reloadTabHeaderWithDic:dic];

            }
                    } failedBlock:^{
            
        } needLog:NO];
        
    }
}

//自己的参与号码
- (IBAction)selfCanyuBtn:(id)sender {
    HistoryNumView *his = [[HistoryNumView alloc]initWithPdcId:self.pdc_id AndQiShu:self.pdc_qishu AndTitle:titleLab.text Owner:[UserInfo getUserID]];
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:his];
    
}
//计算详细 进入开奖规则界面
- (IBAction)jsxxBtnOnClick:(id)sender {
    WYPublicWeb *web = [[WYPublicWeb alloc]init];
    web.mtitle = @"计算详情";
    NSString *newIp = [DBB_IP stringByReplacingOccurrencesOfString:@"?/" withString:@""];
    web.urlStr = [NSString stringWithFormat:@"%@%@%@",newIp,JSXX_URL,self.pdc_id];
    [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:web animated:YES];
}
@end
